package com.qxsx.midi.activity.personal;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.activity.home.ExamListActivity;
import com.qxsx.midi.activity.main.DownLodeActivity;
import com.qxsx.midi.activity.main.MainActivity;
import com.qxsx.midi.adapter.CateAdapter;
import com.qxsx.midi.adapter.DownlodeMusicAdapter;
import com.qxsx.midi.adapter.MyAdapter;
import com.qxsx.midi.config.DataManager;
import com.qxsx.midi.db.MyMusicFileDBTool;
import com.qxsx.midi.model.MyFile;
import com.qxsx.midi.model.MyMusicFile;
import com.qxsx.midi.utils.Tools;
import com.qxsx.midi.view.ZListView.ZListView;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2016/1/19.
 */
public class DownLodeMusciActivity extends BaseActivity implements View.OnClickListener {
    private ZListView lv_downlode;
    private DownlodeMusicAdapter myAdapter; // lv_files的适配器
    private List<MyMusicFile> data; // lv_file的数据源

    private View mPopupWindowView;
    private static PopupWindow mPopupWindow;

    //分类
    private ListView lv_cate;
    private CateAdapter cateAdapter;
    private static String grade = ""; //初始化等级
    private static String major = ""; //初始化专业
    private static int LEFT_P;  //左边POP选中位置
    private static int RIGHT_P; //右边POP选中位置

    private TextView tv_major, tv_grade;
    private LinearLayout ll_major, ll_grade;
    private TextView tv_major_arrow, tv_grade_arrow;
    private Handler mHandler;  //执行下拉、上拉线程


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_downlode_music);
        setTitle("音频下载");
        setNavBtn(R.mipmap.back, 0);
        findById();
        mHandler = new Handler();
        lv_downlode.setPullLoadEnable(false);
        lv_downlode.setPullRefreshEnable(false);

        MyMusicFileDBTool myFileDBTool = null; // 执行对数据库的相关操作
        myFileDBTool = new MyMusicFileDBTool(DownLodeMusciActivity.this);
        data = myFileDBTool.getAll(); // 数据源，从数据库中读取而来
        myAdapter = new DownlodeMusicAdapter(DownLodeMusciActivity.this, data, DownLodeMusciActivity.this);
        lv_downlode.setAdapter(myAdapter); // ListView设置适配器
        Log.e("222222222", myFileDBTool.getAll() + "");

//        Tools.GetVideoFileName(getDiskCacheDir(DownLodeMusciActivity.this) + "/Music");
//        Log.e("11111111111111111111111", Tools.GetVideoFileName(getDiskCacheDir(DownLodeMusciActivity.this) + "/Music") + "");


    }

    private void findById() {
        lv_downlode = (ZListView) findViewById(R.id.lv_downlode);

        tv_major = (TextView) findViewById(R.id.tv_major);
        tv_major.setOnClickListener(this);

        tv_grade = (TextView) findViewById(R.id.tv_grade);
        tv_grade.setOnClickListener(this);

        ll_major = (LinearLayout) findViewById(R.id.ll_major);
        ll_major.setOnClickListener(this);

        ll_grade = (LinearLayout) findViewById(R.id.ll_grade);
        ll_grade.setOnClickListener(this);

        tv_grade_arrow = (TextView) findViewById(R.id.tv_grade_arrow);
        tv_major_arrow = (TextView) findViewById(R.id.tv_major_arrow);


        LEFT_P = DataManager.getManage().getMajorList().size() - 1;
        RIGHT_P = DataManager.getManage().getGradeList().size() - 1;
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
        }

    }


    /**
     * 取缓存地址
     *
     * @param context 获取到的系统路径
     * @param
     * @return
     */
    public String getDiskCacheDir(Context context) {
        String cachePath;
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            cachePath = context.getExternalCacheDir().getPath();
        } else {
            cachePath = context.getCacheDir().getPath();
        }
        // 将系统的路径和唯一值拼接
        return cachePath;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_major:
                // 获取自定义布局文件视图
                mPopupWindowView = LayoutInflater.from(DownLodeMusciActivity.this).inflate(R.layout.major_pop_item, null, false);
                lv_cate = (ListView) mPopupWindowView.findViewById(R.id.lv_cate);
                // 创建PopupWindow实例,LayoutParams.MATCH_PARENT分别是宽度和高度
                mPopupWindow = new PopupWindow(mPopupWindowView, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, true);
                ColorDrawable dw = new ColorDrawable(DownLodeMusciActivity.this.getApplicationContext().getResources().getColor(R.color.theme_transparency));
                mPopupWindow.setBackgroundDrawable(dw);
                // 这里是位置显示方式,在屏幕的右侧,v(标题栏)之下
                mPopupWindowView.setFocusable(true);
                mPopupWindowView.setFocusableInTouchMode(true);
                mPopupWindow.setBackgroundDrawable(DownLodeMusciActivity.this.getResources().getDrawable(R.mipmap.study_call_menu_bg));
                mPopupWindowView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (mPopupWindow.isShowing()) {
                            mPopupWindow.dismiss();
                        }
                    }
                });
                mPopupWindowView.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                        if (arg1 == KeyEvent.KEYCODE_BACK) {
                            if (mPopupWindow != null) {
                                mPopupWindow.dismiss();
                            }
                        }
                        return false;
                    }
                });
                cateAdapter = new CateAdapter(DownLodeMusciActivity.this, DataManager.getManage().getMajorList(), "LEFT");
                lv_cate.setAdapter(cateAdapter);
                cateAdapter.setChoose(LEFT_P);
                lv_cate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        cateAdapter.setChoose(position);
                        tv_major.setText(DataManager.getManage().getMajorList().get(position).getMajor_name());
                        LEFT_P = position;
                        if (mPopupWindow != null) {
                            major = DataManager.getManage().getMajorList().get(position).getMajor_id();
//                            if (major.equals("")) {
//                                MyMusicFileDBTool myFileDBTool = null; // 执行对数据库的相关操作
//                                myFileDBTool = new MyMusicFileDBTool(DownLodeMusciActivity.this);
//                                data = myFileDBTool.getAll(); // 数据源，从数据库中读取而来
//                                myAdapter = new DownlodeMusicAdapter(DownLodeMusciActivity.this, data, DownLodeMusciActivity.this);
//                                lv_downlode.setAdapter(myAdapter); // ListView设置适配器
//                            } else {
                            MyMusicFileDBTool myFileDBTool = new MyMusicFileDBTool(DownLodeMusciActivity.this);
                            data = myFileDBTool.getOne1(major, grade); // 数据源，从数据库中读取而来
                            myAdapter = new DownlodeMusicAdapter(DownLodeMusciActivity.this, data, DownLodeMusciActivity.this);
                            lv_downlode.setAdapter(myAdapter); // ListView设置适配器
//                            }
                        }
                        mPopupWindow.dismiss();
                    }
                });
                mPopupWindow.showAsDropDown(v, 0, 5);
                mPopupWindow.setFocusable(true);
                mPopupWindow.setOutsideTouchable(true);
                tv_major.setSelected(true);
                tv_grade.setSelected(false);
                ll_major.setClickable(true);
                tv_major_arrow.setSelected(true);
                tv_grade_arrow.setSelected(false);
                ll_grade.setClickable(false);
                break;
            case R.id.tv_grade:

                // 获取自定义布局文件视图
                mPopupWindowView = LayoutInflater.from(DownLodeMusciActivity.this).inflate(R.layout.major_pop_item, null, false);
                lv_cate = (ListView) mPopupWindowView.findViewById(R.id.lv_cate);
                // 创建PopupWindow实例,LayoutParams.MATCH_PARENT分别是宽度和高度
                mPopupWindow = new PopupWindow(mPopupWindowView, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, true);
                ColorDrawable dw2 = new ColorDrawable(DownLodeMusciActivity.this.getApplicationContext().getResources().getColor(R.color.theme_transparency));
                mPopupWindow.setBackgroundDrawable(dw2);
                // 这里是位置显示方式,在屏幕的右侧,v(标题栏)之下
                mPopupWindowView.setFocusable(true);
                mPopupWindowView.setFocusableInTouchMode(true);
                mPopupWindow.setBackgroundDrawable(DownLodeMusciActivity.this.getResources().getDrawable(R.mipmap.study_call_menu_bg));
                mPopupWindowView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (mPopupWindow.isShowing()) {
                            mPopupWindow.dismiss();
                        }
                    }
                });
                mPopupWindowView.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                        if (arg1 == KeyEvent.KEYCODE_BACK) {
                            if (mPopupWindow != null) {
                                mPopupWindow.dismiss();
                            }
                        }
                        return false;
                    }
                });
                mPopupWindow.showAsDropDown(v, 0, 5);
                mPopupWindow.setFocusable(true);
                mPopupWindow.setOutsideTouchable(true);
                cateAdapter = new CateAdapter(DownLodeMusciActivity.this, DataManager.getManage().getGradeList(), "RIGHT");
                lv_cate.setAdapter(cateAdapter);
                cateAdapter.setChoose(RIGHT_P);
                lv_cate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        cateAdapter.setChoose(position);
                        tv_grade.setText(DataManager.getManage().getGradeList().get(position).getMajor_name());
                        RIGHT_P = position;
                        if (mPopupWindow != null) {
                            grade = DataManager.getManage().getGradeList().get(position).getMajor_id();
//                            if (grade.equals("")) {
//                                MyMusicFileDBTool myFileDBTool = null; // 执行对数据库的相关操作
//                                myFileDBTool = new MyMusicFileDBTool(DownLodeMusciActivity.this);
//                                data = myFileDBTool.getAll(); // 数据源，从数据库中读取而来
//                                myAdapter = new DownlodeMusicAdapter(DownLodeMusciActivity.this, data, DownLodeMusciActivity.this);
//                                lv_downlode.setAdapter(myAdapter); // ListView设置适配器
//                            } else {
                            MyMusicFileDBTool myFileDBTool = null; // 执行对数据库的相关操作
                            myFileDBTool = new MyMusicFileDBTool(DownLodeMusciActivity.this);
                            data = myFileDBTool.getOne1(major, grade); // 数据源，从数据库中读取而来
                            myAdapter = new DownlodeMusicAdapter(DownLodeMusciActivity.this, data, DownLodeMusciActivity.this);
                            lv_downlode.setAdapter(myAdapter); // ListView设置适配器
                        }
//                        }
                        mPopupWindow.dismiss();
                    }
                });
                tv_major.setSelected(false);
                tv_grade.setSelected(true);
                ll_major.setClickable(false);
                tv_major_arrow.setSelected(false);
                tv_grade_arrow.setSelected(true);
                ll_grade.setClickable(true);
                break;
            case R.id.ll_major:

                break;
            case R.id.ll_grade:

                break;
        }
    }
}
